using System;
using System.Drawing;
using System.IO;

namespace LeanUi.ImageBundle
{
    public class ImageRect
    {
        public String Name { get; private set; }
        public String BundleName { get; private set; }
        public String FullPath { get; private set; }
        public Bitmap Image { get; private set; }
        public bool HasBeenPositioned { get; private set; }
        public int Left { get; private set; }
        public int Top { get; private set; }
        public int Height { get; private set; }
        public int Width { get; private set; }

        public ImageRect(FileInfo file)
        {
            if (file == null) throw new ArgumentNullException("file");

            Name = NamingHelper.GetNameFor(file.Name);
            BundleName = NamingHelper.GetBundleNameFor(file.Directory.Name);
            FullPath = file.FullName;
            Image = new Bitmap(FullPath);
            Width = Image.Width;
            Height = Image.Height;
            HasBeenPositioned = false;
        }

        public void SetPosition(int left, int top)
        {
            HasBeenPositioned = true;
            Left = left;
            Top = top;
        }

        public bool IsDisabledSprite()
        {
            var spritename = NamingHelper.GetSpriteClassFor(this.Name);
            return spritename.EndsWith("DisabledSprite");
        }

        public BackgroundType BackgroundType
        {
            get
            {
                if (this.Width < 3 && this.Height > 8)
                {
                    return BackgroundType.Vertical;
                }
                if (this.Height < 3 && this.Width > 8)
                {
                    return BackgroundType.Horizontal;
                }
                return BackgroundType.None;
            }
        }

        public override string ToString()
        {
            return string.Format("Name: {0}, Width: {1}, Height: {2}, Top: {3}, Left: {4}",
                                 Name, Width, Height, Top, Left);
        }
    }
}